// pages/recharge/index.js
const app = getApp();
const api = require("../../utils/api.js")
import cookies from '../../plugins/weapp-cookie/weapp-cookie'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    monthMoney: 60,
    yearMoney: 300,
    everyDayMoney: 2,
    everyMonthMoney: 1,
    selectType: '',
    list: [],
    selectClass: 'sg-select',
    totalMoney: 0,
    userInfo: null,
    selectedIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      userInfo: app.globalData.userInfo
    })
    
    wx.showLoading({
      title: '加载中',
    })
    wx.requestWithCookie({
      url: api.getCommodityUrl,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        MerchantCode: that.data.userInfo.Agents,
        CommodityType: 21
      },
      success: function (res) {
        if (res.data.Code === 1) {
          that.setData({
            list: res.data.Entity
          })
          that.updatePayMoney()
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  updatePayMoney: function() {
    var that = this;
    if (that.data.list.length > 0) {
      that.setData({
        totalMoney: that.data.list[that.data.selectedIndex].MoneyNum * 100
      })
    }
    
  },
  buyVIP: function() {
    var that = this;
    wx.showLoading({
      title: '支付中',
    })
    var id = that.data.list[that.data.selectedIndex]['Id'];
    wx.requestWithCookie({
      url: api.getPayParamUrl,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        ProductId: id
      },
      success: function (res) {
        // console.log(res)
        if (res.data.Code === 1) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function(info) {
              if (info.errMsg == "requestPayment:ok") {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            }
          })
        }
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  selectType: function(event) {
    var index = event.currentTarget.dataset.index;
    this.setData({
      selectedIndex: index
    });
    this.updatePayMoney()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})